package org.net.cms.service.leave;

import jakarta.annotation.Resource;
import org.net.cms.controller.app.leave.vo.AppStudentLeaveReqVO;
import org.net.cms.controller.app.leave.vo.AppStudentLeaveRespVO;
import org.net.cms.dao.ParentAccountDto;
import org.net.cms.dao.StudentLeaveDto;
import org.net.cms.mapper.ParentAccountMapper;
import org.net.cms.mapper.StudentLeaveMapper;
import org.net.cms.mapper.StudentMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentLeaveServiceImpl implements StudentLeaveService{

    @Resource
    private StudentLeaveMapper studentLeaveMapper;

    @Resource
    private ParentAccountMapper parentAccountMapper;

    @Resource
    private StudentMapper studentMapper;

    @Override
    public Boolean insert(AppStudentLeaveReqVO studentLeaveReqVO) {
        int parentId = getParentIdByPhone();
        // 获取当前用户的电话号码
        String phone = parentAccountMapper.getParentAccountById(parentId).getPhone();
        String name = studentMapper.getStudent(studentLeaveReqVO.getStudentId()).getName();

        StudentLeaveDto studentLeaveDto = new StudentLeaveDto();
        studentLeaveDto.setId(studentLeaveReqVO.getId());
        studentLeaveDto.setParentId(parentId);
        studentLeaveDto.setStudentId(studentLeaveReqVO.getStudentId());
        studentLeaveDto.setPhone(phone);
        studentLeaveDto.setName(name);
        studentLeaveDto.setDate(studentLeaveReqVO.getDate());
        studentLeaveDto.setReason(studentLeaveReqVO.getReason());
        studentLeaveDto.setStatus(0);

        studentLeaveMapper.insert(studentLeaveDto);
        return Boolean.TRUE;
    }

    /**
     * 通过当前账号的电话号码，获取用户id
     * @return
     */
    private int getParentIdByPhone() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String phone = userDetails.getUsername();
        return parentAccountMapper.getParentAccountByPhone(phone).getId();
    }

    @Override
    public Boolean delete(int id) {
        studentLeaveMapper.deleteLeave(id);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateLeave(AppStudentLeaveRespVO studentLeaveReqVO) {
        studentLeaveReqVO.setParentId(getParentIdByPhone());
        studentLeaveReqVO.setStatus(0);
        studentLeaveMapper.updateLeave(studentLeaveReqVO);
        return Boolean.TRUE;
    }

    @Override
    public AppStudentLeaveRespVO findById(int id) {
        StudentLeaveDto byId = studentLeaveMapper.findById(id);

        AppStudentLeaveRespVO appStudentLeaveRespVO = new AppStudentLeaveRespVO();
        appStudentLeaveRespVO.setId(byId.getId());
        appStudentLeaveRespVO.setParentId(byId.getParentId());
        appStudentLeaveRespVO.setStudentId(byId.getStudentId());
        appStudentLeaveRespVO.setPhone(byId.getPhone());
        appStudentLeaveRespVO.setName(byId.getName());
        appStudentLeaveRespVO.setDate(byId.getDate());
        appStudentLeaveRespVO.setReason(byId.getReason());
        appStudentLeaveRespVO.setStatus(byId.getStatus());

        return appStudentLeaveRespVO;
    }

    @Override
    public List<StudentLeaveDto> findAll() {
        return studentLeaveMapper.getAll(getParentIdByPhone());
    }

    @Override
    public List<StudentLeaveDto> findListByType(int type) {
        return studentLeaveMapper.findAllByStatus(type);
    }

    @Override
    public Boolean updateStatus(int id, int status) {
        studentLeaveMapper.updateStatus(id, status);
        return Boolean.TRUE;
    }
}
